package com.xuecheng.content.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.commons.model.vo.CoursePubVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.content.domain.CoursePub;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CoursePubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程发布前端控制器
 * </p>
 *
 * @author itheima
 */
@Slf4j
@RestController  //RestController响应数据是一个JSON字符串
@RequestMapping("/coursePub")
public class CoursePubController {

    @Autowired
    private CoursePubService  coursePubService;

    //课程预览
    @GetMapping("preview/{courseBaseId}")
    public ModelAndView preview (@PathVariable("courseBaseId") Long courseBaseId) {
        //创建ModelAndView对象
        ModelAndView mv = new ModelAndView();
        //指定模板
        mv.setViewName("learing_article");
        //指定数据
        Map map = coursePubService.preview(courseBaseId);
        mv.addAllObjects(map);
        //返回
        return mv;
    }

    /**
     * 课程发布
     * 请求地址:post
     * 路径参数
     */
    @PostMapping("publish/{courseBaseId}")
    public ResponseResult publish (@PathVariable("courseBaseId") Long courseBaseId) throws Exception {
        return coursePubService.publish(courseBaseId);
    }

    //查询已经发布的课程数据
    @GetMapping("findAll")
    public ResponseResult findAll () {
        //查询所有的课程发布表数据
        LambdaQueryWrapper<CoursePub> qw = new LambdaQueryWrapper<>();
        qw.eq(CoursePub::getStatus,1);
        List<CoursePub> list = coursePubService.list(qw);
        List<CoursePubVo> vos = BeanHelper.copyWithCollection(list, CoursePubVo.class);
        return ResponseResult.okResult(vos);
    }

    //根据id查询已发布的课程
    @GetMapping("{id}")
    public ResponseResult findById (@PathVariable("id") Long id) {
        CoursePub coursePub = coursePubService.getById(id);
        CoursePubVo vo = BeanHelper.copyProperties(coursePub, CoursePubVo.class);
        return ResponseResult.okResult(vo);
    }
}
